package com.liang.security.test;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @Author: liangjy
 * @Date:2022/2/7 23:43
 * @Package_name: com.liang.security.test
 * @Description:
 */
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Thread)
public class BasicType {
    @Benchmark
    public int intFinalSum() {
        final int a=34;
        final int b=30;
        return a+b;
    }


    @Benchmark
    public int intSum() {
        int a=34;
        int b=30;
        return a+b;
    }

    @Benchmark
    public User getFinalUser() {
        final User user = new User();
        user.setAge(12);
        user.setName("测试");
        user.setId(new Random().nextInt());
        return user;
    }
    @Benchmark
    public User getUser() {
        User user = new User();
        user.setAge(12);
        user.setName("测试");
        user.setId(new Random().nextInt());
        return user;
    }



    public static void main(String[] args)throws Exception {
        // 使用一个单独进程执行测试，执行5遍warmup，然后执行5遍测试
        Options opt = new OptionsBuilder()
                .include(".*" +BasicType.class.getSimpleName()+ ".*")
                .forks(1)
                .warmupIterations(5) //预热次数
                .measurementIterations(10)
                .build();
        new Runner(opt).run();
    }

}
